//
//  ByOrderDetailVC.m
//  hutouben_ios
//
//  Created by mc on 16/12/1.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByOrderDetailVC.h"
#import "ByNetOrderModel.h"
#import "ByOrderDetailCell.h"

@interface ByOrderDetailVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ByNetOrderModel *detailModel;
@end

@implementation ByOrderDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单详情";
    [self addLeftBackBarButtonItem];
    [self initTableView];
    [self fetchData];
}
#pragma mark ---获取数据
-(void)fetchData{
    self.detailModel = [[ByNetOrderModel alloc] init];
    self.detailModel.post_price = @"免运费";
    self.detailModel.price = @"¥1000";
    self.detailModel.order_code = @"P1232r3wr3r76";
    self.detailModel.createtime = @"2016-11-30";
    ByNetOrderItemModel *model = [[ByNetOrderItemModel alloc] init];
    model.name = @"这是订单的商品标题，两行，字体微软雅黑，字号24.色值545454";
    model.price = @"¥106";
    model.count = @"×34";
    
    ByNetOrderItemModel *model1 = [[ByNetOrderItemModel alloc] init];
    model1.name = @"这是订单的商品标题，两行，字体微软雅黑，字号24.色值545454";
    model1.price = @"¥106";
    model1.count = @"×34";

    self.detailModel.items = @[model,model1];
    [self.tableView reloadData];
}
#pragma mark ---创建视图
- (void)initTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = HTB_BG_GRAY;
    _tableView.delegate = self;
    _tableView.dataSource = self;
}

#pragma mark ---UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.detailModel.items.count + 2;
    }
    else
        return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        NSInteger itemsCount = self.detailModel.items.count;
        if (indexPath.row < itemsCount) {
            return 100;
        }else
            return 35;
    }else
        return 35;
        
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    /**通用Cell*/
    static NSString *identifer = @"orderDetailCell";
    UITableViewCell *customCell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (customCell == nil) {
        customCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    for (UIView *subviews in customCell.subviews) {
        [subviews removeFromSuperview];
    }
    UILabel *titleLabel = [[UILabel alloc] init];
    NSString *titleStr = @"商品总额：";
    CGFloat width = [titleStr getSizeOfString:14].width;
    [customCell addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(customCell).offset(10);
        make.top.equalTo(customCell).offset(10);
        make.size.mas_equalTo(CGSizeMake(width, 15));
    }];
    titleLabel.font = FONT(14);
    titleLabel.textColor = HTB_TEXT_COLOR2;
    
    UILabel *subLabel = [[UILabel alloc] init];
    [customCell addSubview:subLabel];
    width = SCREEN_WIDTH - width - 30;
    [subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_right).offset(10);
        make.top.equalTo(customCell).offset(10);
        make.size.mas_equalTo(CGSizeMake(width, 15));
    }];
    subLabel.font = FONT(14);
    subLabel.textAlignment = NSTextAlignmentRight;
    
    
    /***商品Cell*/
    if (indexPath.section == 0) {
        NSInteger itemsCount = self.detailModel.items.count;
        if (indexPath.row < itemsCount) {
            ByOrderDetailCell *cell = [ByOrderDetailCell cellWithTableView:tableView];
            ByNetOrderItemModel *model = self.detailModel.items[indexPath.row];
            cell.itemModel = model;
            return cell;
        }
        
        else if(indexPath.row == itemsCount){
            titleLabel.text = @"商品总额：";
            subLabel.text = @"¥174";
            subLabel.textColor = HTB_TEXT_COLOR2;
            UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 34, SCREEN_WIDTH - 20, 1)];
            lineView.backgroundColor = HTB_LINE_COLOR;
            [customCell addSubview:lineView];
            return customCell;

        }
        else{
            titleLabel.text = @"运费";
            subLabel.text = @"免运费";
            subLabel.textColor = HTB_TEXT_COLOR2;
            return customCell;
        }
        

    }
    /***总计Cell*/
    else{
           titleLabel.text = @"总计：";
           subLabel.text = @"¥174";
           subLabel.textColor = HTB_TEXT_COLOR2;
           return customCell;
    }
    
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 15 + 75;

    }else
        return 15;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 90)];
    headerView.backgroundColor = HTB_BG_GRAY;
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 15, SCREEN_WIDTH, 75)];
    contentView.backgroundColor = HTB_BG_WHITE;
    [headerView addSubview:contentView];
    //订单号
    UILabel *codeLabel = [[UILabel alloc] init];
    codeLabel.font = FONT(14);
    [contentView addSubview:codeLabel];
    NSString *codeStr = [NSString stringWithFormat:@"订单号：%@",self.detailModel.order_code];
    CGFloat width = [codeStr getSizeOfString:14].width + 10;
    [codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentView).offset(10);
        make.top.equalTo(contentView).offset(5);
        make.size.mas_equalTo(CGSizeMake(width, 20));
    }];
    codeLabel.text = codeStr;
    
    //时间
    UILabel *timeLabel = [[UILabel alloc] init];
    [contentView addSubview:timeLabel];
    width = SCREEN_WIDTH - codeLabel.right - 30;
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(codeLabel).offset(10);
        make.top.equalTo(contentView).offset(5);
        make.size.mas_equalTo(CGSizeMake(width, 20));
    }];
    timeLabel.text = self.detailModel.createtime;
    timeLabel.textColor = HTB_TEXT_COLOR3;
    timeLabel.font = FONT(14);
    timeLabel.textAlignment = NSTextAlignmentRight;
    
 
    
    //线
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = HTB_LINE_COLOR;
    [contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentView).offset(0);
        make.top.equalTo(codeLabel.mas_bottom).offset(4);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 1));
    }];
    
    
    //订单状态
    UILabel *statusLabel = [[UILabel alloc] init];
    [contentView addSubview:statusLabel];
    NSString *status = @"订单状态";
    [statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentView).offset(10);
        make.top.equalTo(lineView.mas_bottom).offset(10);
//        make.size.mas_equalTo()

    }];
    
    //订单状态详情
    UILabel *detailStatus = [[UILabel alloc] init];
    [contentView addSubview:detailStatus];
    [detailStatus mas_makeConstraints:^(MASConstraintMaker *make) {
        
    }];
    
    //线
    UIView *lineView1 = [[UIView alloc] init];
    [contentView addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
    }];
    
    
    return headerView;
}

@end
